Combining Option and Iterable in for comprehensions


In [2]:
val xOpt = Option(1)
val values = Array(1, 2, 3)
for {
    x <- xOpt
    value <- values
} yield value


Main.scala:36: type mismatch;
 found   : Array[Int]
 required: Option[?]
    value <- values
          ^

In [3]:
for {
    x <- xOpt.toSeq  // need 
    value <- values
} yield value


res2: Seq[Int] = List(1, 2, 3)

In [6]:
Option(1).toSeq


res5: Seq[Int] = List(1)

In [5]:
None.toSeq


res4: Seq[Nothing] = List()

In [ ]: